package com.yjs.ppjokedemo.ui.find;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.snackbar.Snackbar;
import com.yjs.ppjokedemo.R;
import com.yjs.ppjokedemo.adapter.RecomListAdapter;
import com.yjs.ppjokedemo.bean.RecomProduct;
import com.yjs.ppjokedemo.databinding.FragmentRecommendBinding;
import com.yjs.ppjokedemo.viewmodel.RecomViewModel;

import java.util.List;


public class RecommendFragment extends Fragment implements RecomListAdapter.RecomInterface {

    FragmentRecommendBinding mRecommendBinding;
    private RecomViewModel mRecomViewModel;
    private RecomListAdapter mRecomListAdapter;


    public RecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mRecommendBinding = FragmentRecommendBinding.inflate(inflater,container,false);
        return mRecommendBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRecomListAdapter = new RecomListAdapter(this);
        mRecommendBinding.rvRecommend.setAdapter(mRecomListAdapter);

        mRecomViewModel = new ViewModelProvider(requireActivity()).get(RecomViewModel.class);

        mRecomViewModel.getRecomProducts().observe(getViewLifecycleOwner(), new Observer<List<RecomProduct>>() {
            @Override
            public void onChanged(List<RecomProduct> recomProducts) {
                mRecomListAdapter.submitList(recomProducts);
            }
        });
    }

    @Override
    public void addLiked(RecomProduct recomProduct) {

    }

//    @Override
//    public void addLiked(RecomProduct recomProduct) {
//        boolean added = mRecomViewModel.addProductToLiked(recomProduct);
//        if (added){
//            Snackbar.make(requireView(),recomProduct.getName(),Snackbar.LENGTH_LONG).setAction("跳转", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                }
//            })
//        }
//    }
}